<?php
namespace app\dboard\controller;
use think\captcha;
use think\facade\Cookie;
use think\facade\Session;
use think\facade\Url;
use think\Db;
class Students extends Base
{
    public $study_status;
    public function initialize() {
        parent::initialize();
        $returnUrl=Url::build('dboard/mlogin/sign');
        if(!isset($this->AdminAccount['AdminID']) || intval($this->AdminAccount['AdminID'])<=0 || !isset($this->AdminAccount['sid'])){
            if($this->request->isAjax()){
                return $this->print_result(['Code'=>9999,'Msg'=>'请登录']);
            }
            return $this->redirect($returnUrl);
        }
        $this->study_status = [
            1 => '未付款未排课',
            2 => '已入学待排课',
            3 => '已入学已排课',
            4 => '停课',
        ];
    }
    public function add(){

        $this->assign('study_statusarr',$this->study_status);
        return view('students/add');
    }
    public function edit(){
        $input = $this->request->param(false);
        $id = isset($input['cid'])?$input['cid']:0;
        if(!$id){
            $this->res_data['Msg'] = '参数有误';
            return $this->print_result($this->res_data);
        }

        $info = Db::name('user')
                ->where('id',$id)
                ->find();
        $this->assign('info',$info);
        $this->assign('study_statusarr',$this->study_status);
        $this->assign('cid',$id);
        return view('students/edit');
    }
    public function lists(){
        $this->assign('study_statusarr',$this->study_status);
        return view('students/lists');
    }
   public function ajaxList(){
        // if (!$this->request->isAjax()) {
        //     $this->res_data['Msg'] = '请求方式错误';
        //     return $this->print_result($this->res_data);
        // }
        $input = $this->request->param(false);
        $page_limit      = isset($input['length'])?$input['length']:10;
        $page_start      = isset($input['start'])?$input['start']:0;
        $study_status    = isset($input['study_status'])?$input['study_status']:0;
        $p = 1;
        if ($page_start > 0) {
            $p = ceil($page_start / ($page_limit - 1));
        }
        $sid = isset($input['sid'])?$input['sid']:0;
        if (!$sid) {
          $this->res_data['Msg'] = "缺少必填信息";
          return $this->res_data;
        }
        $store= Db::name('store')->where([['id','=',$sid]])->find();
        if (!$store) {
          $this->res_data['Msg'] = "无此门店";
          return $this->res_data;
        }
        $Students = Controller('Students', 'logic');
        list($list,$recordsTotal)   = $Students->ajaxList($sid,$study_status,$this->study_status,$page_start,$page_limit);
        $this->res_data['Msg']='OK';
        $this->res_data['Code']=0;
        $this->res_data['Data']=['students'=>$list,'recordsTotal'=>$recordsTotal,'Data'=>$list];
        return $this->print_result($this->res_data);
   }
    public function infoData(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($this->res_data);
        }
        $input = $this->request->param(false);
        $id = isset($input['cid'])?$input['cid']:0;
        if(!$id){
            $this->res_data['Msg'] = '参数有误';
            return $this->print_result($this->res_data);
        }
        $where = [];
        $where[] = ['id','=',$id];
        $info = Db::name('user')
                ->where($where)
                ->find();

        $this->res_data['Msg']='OK';
        $this->res_data['Code']=0;
        $this->res_data['Data']=['students'=>$info];
        return $this->print_result($this->res_data);
   }
   public function doCreateStudents(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($result);
        }
        $param    = $this->request->param(false);
        $Students = Controller('Students', 'logic');
        $result            = $Students->create($param);
        return $this->print_result($result);
   }
   public function editStudentsinfo(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($result);
        }
        $param    = $this->request->param(false);
        $Students= Controller('Students', 'logic');
        $result            = $Students->edit($param);
        return $this->print_result($result);
   }
   public function delete(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($result);
        }
        $param    = $this->request->param(false);
        $Students = Controller('Students', 'logic');
        $result            = $Students->delete($param);
        return $this->print_result($result);
   }
   public function export()
   {
        $input = $this->request->param(false);
        $study_status    = isset($input['study_status'])?$input['study_status']:0;
        $sid = isset($input['sid'])?$input['sid']:0;
        if (!$sid) {
          $this->res_data['Msg'] = "缺少必填信息";
          return $this->res_data;
        }
        $store= Db::name('store')->where([['id','=',$sid]])->find();
        if (!$store) {
          $this->res_data['Msg'] = "无此门店";
          return $this->res_data;
        }
        $Students = Controller('Students', 'logic');
        list($list,$count)   = $Students->ajaxList($sid,$study_status,$this->study_status);

        $fileName = "学员信息" . date("Ymd", time()) . ".xls";
        require_once env('root_path')."vendor/PHPExcel/Classes/PHPExcel.php";
        
        $objPHPExcel = new \PHPExcel();
        $objPHPExcel->getProperties();
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', '学员姓名');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B1', '年龄');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C1', '联系电话');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D1', '家长姓名');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('E1', '所学课程');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('F1', '课时');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('G1', '入学时间');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('H1', '状态');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('I1', '录入时间');
 
        $column = 2;
        $objActSheet = $objPHPExcel->getActiveSheet();
        foreach ($list as $key => $rows) { // 行写入
            $objActSheet->setCellValue('A' . $column, $rows['name']);
            $objActSheet->setCellValue('B' . $column, ' '.$rows['age']);
            $objActSheet->setCellValue('C' . $column, ' '.$rows['phone']);
            $objActSheet->setCellValue('D' . $column, $rows['mother_name'].'-'.$rows['father_name']);
            $objActSheet->setCellValue('E' . $column, $rows['course']);
            $objActSheet->setCellValue('F' . $column, ' '.$rows['times']);
            $objActSheet->setCellValue('G' . $column, $rows['create_time']);
            $objActSheet->setCellValue('H' . $column, $rows['study_status']);
            $objActSheet->setCellValue('I' . $column, $rows['create_time']);
            $column++;
        }
 
        $fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表
 
        $objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表
 
        header('Content-Type: application/vnd.ms-excel');
 
        header("Content-Disposition: attachment;filename=$fileName");
 
        header('Cache-Control: max-age=0');
        require_once env('root_path')."vendor/PHPExcel/Classes/PHPExcel/IOFactory.php";
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
 
        $objWriter->save('php://output'); // 文件通过浏览器下载
 
   }
}
